package com.ruoyi.project.mapper;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.common.domainenum.FinRecordVoucherStatus;
import com.ruoyi.project.domain.FinRecordVoucher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 记账凭证Mapper接口
 *
 * @author abel
 * @date 2024-10-15
 */
public interface FinRecordVoucherMapper extends BaseMapper<FinRecordVoucher>
{

    /**
     * 查询记账凭证列表
     *
     * @param finRecordVoucher 记账凭证
     * @return 记账凭证集合
     */
    List<FinRecordVoucher> selectFinRecordVoucherList(FinRecordVoucher finRecordVoucher);

    /**
     * 删除记账凭证
     *
     * @param id 记账凭证ID
     * @return 结果
     */
    public int deleteFinRecordVoucherById(Long id);

    /**
     * 批量删除记账凭证
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteFinRecordVoucherByIds(Long[] ids);

    @Select("select max(record_no) from fin_record_voucher where package_id=#{packageId} and record_tag=#{recordTag} " +
            " and year_value=#{currentYear} and month_value=#{currentMonth} ")
    Integer getMaxRecordNo(@Param("packageId") Long packageId, @Param("recordTag")String recordTag,
                           @Param("currentYear")Integer currentYear, @Param("currentMonth")Integer currentMonth);

    @Select("select * from fin_record_voucher where package_id=#{packageId} and record_tag=#{recordTag} and year_value=#{currentYear} " +
            "and month_value=#{currentMonth} and record_no=#{recordNo}")
    FinRecordVoucher findByRecordNo(@Param("packageId") Long packageId, @Param("recordTag")String recordTag,
                           @Param("currentYear")Integer currentYear, @Param("currentMonth")Integer currentMonth, @Param("recordNo")Integer recordNo);

    @Update("update fin_record_voucher set status=#{status},check_id=#{checkId},check_by=#{checkBy},check_date=#{checkDate} where id=#{id}")
    int updateToChecked(@Param("id")Long id, @Param("status")FinRecordVoucherStatus status, @Param("checkId") Long checkId,  @Param("checkBy")String checkBy, @Param("checkDate") Date checkDate);

    @Update("update fin_record_voucher set status=#{status},check_id=null,check_by='',check_date=null where id=#{id}")
    int updateToDraft( @Param("id")Long id, @Param("status") FinRecordVoucherStatus status);

    @Update("update fin_record_voucher set status=#{status},record_id=#{recordId},record_by=#{recordBy},record_to_book_date=#{recordToBookDate} where id=#{id}")
    int updateToRecorded(@Param("id")Long id, @Param("status")FinRecordVoucherStatus status, @Param("recordId") Long recordId
            ,  @Param("recordBy")String recordBy, @Param("recordToBookDate") Date checkDate);

    @Update("update fin_record_voucher set status=#{status},record_id=null,record_by='',record_to_book_date=null where id=#{id}")
    int updateRecordedBack( @Param("id")Long id, @Param("status") FinRecordVoucherStatus status);

    @Select("select * from fin_record_voucher where package_id=#{packageId} and record_tag=#{recordTag} and year_value=#{currentYear} " +
            "and month_value=#{currentMonth} and record_no> #{recordNo} order by record_no asc limit 1")
    FinRecordVoucher findNextByInfo(@Param("packageId") Long packageId, @Param("recordTag")String recordTag,
                                    @Param("currentYear")Integer currentYear, @Param("currentMonth")Integer currentMonth, @Param("recordNo")Integer recordNo);

    @Select("select * from fin_record_voucher where package_id=#{packageId} and record_tag=#{recordTag} and year_value=#{currentYear} " +
            "and month_value=#{currentMonth} and record_no< #{recordNo} order by record_no desc limit 1")
    FinRecordVoucher findPreByInfo(@Param("packageId") Long packageId, @Param("recordTag")String recordTag,
                                    @Param("currentYear")Integer currentYear, @Param("currentMonth")Integer currentMonth, @Param("recordNo")Integer recordNo);

}
